home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / DragWindowGrid / Source / Application.c next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  3.4 KB  |  183 lines  |  [TEXT/CWIE]

  1. //updates 8/96: Prefix.h added as prefix file for 68K and PPC MC projects; old
  2. //routine names changed; DragWindowGrid.ยต.rsrc created from Sample.r
  3.  
  4. #include <QuickDraw.h>
  5. #include <Dialogs.h>
  6. #include <Fonts.h>
  7. #include <Processes.h>
  8. #include <TextEdit.h>
  9. #include <Events.h>
  10. #include <Menus.h>
  11. #include <Memory.h>
  12. #include <Errors.h>
  13. #include <ToolUtils.h>
  14.  
  15.  
  16.  
  17. void InitApplication(void);
  18. void CreateNewWindow(void);
  19. void MainEventLoop(void);
  20. void MenuCommand(long whaHappened);
  21. void DoAboutBox(void);
  22.  
  23. void PreEventLoop(void);
  24. void PostEventLoop(void);
  25. pascal void DrawWindowContent(short, short, GDHandle, long);
  26. void DrawIt(WindowPtr win);
  27. void DoUpdate(WindowPtr thisWindow);
  28.  
  29.  
  30. void DragWindowGrid(WindowPtr win, Point pt);
  31.  
  32.  
  33. static Boolean gDone;
  34.  
  35.  
  36. /*-------------------------------------------------------------------------------------*/
  37.  
  38. void main()
  39. {
  40.  
  41.     InitApplication();
  42.     PreEventLoop();
  43.     MainEventLoop();
  44. }
  45.  
  46.  
  47. /*-------------------------------------------------------------------------------------*/
  48.  
  49. void InitApplication()
  50. {
  51.     Handle theMenu;
  52.  
  53.     // Toolbox initialization
  54.     MaxApplZone();
  55.     InitGraf(&qd.thePort);
  56.     InitFonts();
  57.     InitWindows();
  58.     InitMenus();
  59.     TEInit();
  60.     InitDialogs(nil);
  61.     InitCursor();
  62.     FlushEvents(0,everyEvent);
  63.     
  64.     // Application initialization
  65.     gDone = false;
  66.     
  67.     theMenu = GetNewMBar(128);
  68.     if ( theMenu == nil )
  69.         goto MenuStuffFailed;
  70.  
  71.     SetMenuBar(theMenu);
  72.     AppendResMenu(GetMenuHandle(128), 'DRVR');
  73.     DrawMenuBar();
  74.  
  75.     return;
  76.     
  77. MenuStuffFailed:
  78.     // If the menu stuff failed, something just ain't right (most likely some 
  79.     // resources are missing.
  80.     gDone = true;
  81.     return;
  82.  
  83. }
  84.  
  85.  
  86. /*-------------------------------------------------------------------------------------*/
  87.  
  88. void DoAboutBox()
  89. {
  90.     (void) Alert(128, nil);
  91. }
  92.  
  93.  
  94. /*-------------------------------------------------------------------------------------*/
  95.  
  96. void MainEventLoop()
  97. {
  98.     EventRecord        theEvent;
  99.     WindowPtr        thisWindow;
  100.     short            clickArea;
  101.     long            menuResult;
  102.     char            charCode;
  103.  
  104.     while ( !gDone )
  105.     {
  106.         if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
  107.         {
  108.             switch (theEvent.what)
  109.             {
  110.                 case mouseDown:
  111.                     clickArea = FindWindow(theEvent.where, &thisWindow);
  112.                     
  113.                     if (clickArea == inDrag)
  114.                     {
  115.                         DragWindowGrid(thisWindow, theEvent.where);
  116.                     }
  117.                     else if ( clickArea == inContent )
  118.                     {
  119.                         if ( thisWindow != FrontWindow() )
  120.                             SelectWindow(thisWindow);
  121.                     }
  122.                     else if (clickArea == inGoAway)
  123.                     {
  124.                         if ( TrackGoAway(thisWindow, theEvent.where) )
  125.                             gDone = true;
  126.                     }
  127.                     else if ( clickArea == inMenuBar ) 
  128.                     {
  129.                         menuResult = MenuSelect(theEvent.where);
  130.                         if ( (menuResult  >> 16) != 0 )
  131.                         {
  132.                             MenuCommand(menuResult);
  133.                             HiliteMenu(0);
  134.                         }
  135.                     }
  136.                     break;
  137.                 case keyDown:
  138.                     charCode = theEvent.message & charCodeMask;
  139.  
  140.                     if ( (theEvent.modifiers & cmdKey) != 0 ) 
  141.                     {    
  142.                         menuResult = MenuKey(charCode);
  143.                 
  144.                         if ( (menuResult  >> 16) != 0 )
  145.                             MenuCommand(menuResult);
  146.                             
  147.                     } 
  148.                     break;
  149.                 case updateEvt:
  150.                     thisWindow = (WindowPtr)theEvent.message;    
  151.                     DoUpdate(thisWindow);
  152.                 
  153.                     break;
  154.                 
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161.  
  162. /*-------------------------------------------------------------------------------------*/
  163.  
  164. void MenuCommand(long whaHappened)
  165. {
  166.     short    menuID, menuItem;
  167.     
  168.     menuID = (whaHappened >> 16);
  169.     menuItem = (whaHappened & 0xFFFF);
  170.     
  171.     if ( menuID == 128 )
  172.     {
  173.         if ( menuItem == 1)
  174.             DoAboutBox();
  175.     }
  176.     else if ( menuID == 129 )
  177.     {
  178.         if (menuItem == 1)
  179.             gDone = true;
  180.     }
  181. }
  182.  
  183.